package com.df.bpm.utils.businessViewer.entityTypeDialogServices;

import com.df.bpm.entity.EntityType;
import com.df.bpm.utils.businessViewer.Main;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class EntityTypeService {
    Map<Integer,EntityType> typeMap=new HashMap<>();

    public EntityType get(Integer typeID) {
        EntityType result=typeMap.get(typeID);
        if(result==null){//如果本地没存就去接口获取
            RestTemplate restTemplate = new RestTemplate();
            String url=Main.server+"entityType/id?entityTypeIndex="+typeID;
            ResponseEntity<EntityType> response = restTemplate.exchange(
                    url,
                    HttpMethod.GET,
                    null, // 或者使用 HttpEntity 来添加请求头等信息
                    new ParameterizedTypeReference<EntityType>() {}
            );
            result=response.getBody();
            typeMap.put(typeID,result);
        }
        return result;
    }

    public List<EntityType> getList4Like(EntityType temp) {
        RestTemplate restTemplate = new RestTemplate();
        String tempString=Main.printNonNullFields(temp);
        String url=Main.server+"entityType/entityType4Like?"+tempString;
        ResponseEntity<List<EntityType>> response = restTemplate.exchange(
                url,
                HttpMethod.GET,
                null, // 或者使用 HttpEntity 来添加请求头等信息
                new ParameterizedTypeReference<List<EntityType>>() {}
        );
        return response.getBody();
    }

    public Integer update(EntityType temp) {
        if(temp.getName()!=null
                && !temp.getName().isEmpty()
        ){
            RestTemplate restTemplate = new RestTemplate();
            String url=Main.server+"entityType/";
            HttpEntity<EntityType> request = new HttpEntity<>(temp, null);
            ResponseEntity<?> response = restTemplate.exchange(
                    url,
                    HttpMethod.PUT,
                    request, // 或者使用 HttpEntity 来添加请求头等信息
                    Integer.class
            );
            return (Integer) response.getBody();
        }
        return null;
    }

}
